package com.xuzimian.globaldemo.spring.core.jpa.proxy;

import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import static java.lang.System.out;

@Component
public class JpaRepositoryFactory {

    public JpaRepository createRepository() {
        return (JpaRepository) Proxy.newProxyInstance(JpaRepository.class.getClassLoader(),
                new Class[]{JpaRepository.class}, new JdkHandler());
    }

    class JdkHandler implements InvocationHandler {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args)
                throws Throwable {
            out.println("do something ...");
            return null;
        }
    }
}